package sg.edu.nus.iss.stockex;

import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;

public class CodeDateKeyComparator extends WritableComparator {

	protected CodeDateKeyComparator() {
		super(CodeDateKey.class, true);
	}

	@Override
	public int compare(WritableComparable key1, WritableComparable key2) {
		
		CodeDateKey codeDateKey1 = (CodeDateKey) key1;
		CodeDateKey codeDateKey2 = (CodeDateKey) key2;

		//System.out.println("%%% " + codeDateKey1.getTimestamp() + " compare " + codeDateKey2.getTimestamp());
		
		
		int cmp = codeDateKey1.getCode().compareTo(codeDateKey2.getCode());
		if (cmp != 0) {
			return cmp;
		}

		return codeDateKey1.getTimestamp() == codeDateKey2.getTimestamp() ? 0 : (codeDateKey1
				.getTimestamp() < codeDateKey2.getTimestamp() ? -1 : 1);

	}

}